//
//  ByCellOrderEvaluateFirst.m
//  itboye_shop_ios
//
//  Created by mc on 17/2/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellOrderEvaluateFirst.h"
#import "ByViewTextViewPlaceholder.h"
@interface ByCellOrderEvaluateFirst()
{
    UIImageView                *_proImgView;      // 商品图片
    UIView                     *_lineView;        // 线
    ByViewTextViewPlaceholder  *_contentTextView; // 内容评价
    NSInteger                   _selectTag;       // 用来记录当前选中的button
}
@end
@implementation ByCellOrderEvaluateFirst

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellOrderEvaluateFirst";
    ByCellOrderEvaluateFirst *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        [self setViews];
    }
    return self;
}

-(void)setViews{
    _proImgView = [[UIImageView alloc] init];
    [self addSubview:_proImgView];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self addSubview:_lineView];
    
    _contentTextView = [[ByViewTextViewPlaceholder alloc] init];
    _contentTextView.placeholder = @"宝贝满足你的期待吗？说说它的优点和美中不足的地方吧";
    [self addSubview:_contentTextView];

}

-(void)setModel:(ByModelOrderListItemProduct *)model{
    _model = model;
    
    CGFloat x,y,width,height;
    //商品图片
    x      = 10;
    y      = 10;
    width  = 30;
    height = 30;
    _proImgView.frame = CGRectMake(x, y, width, height);
    [_proImgView setImageWithURL:[NSURL URLWithString:_model.img] placeholderImage:DEFAULT_IMAGE];
    
    //好、中、差评
    width = (SCREEN_WIDTH - 40 - 30)/3;
    NSArray *titleAry = @[@"好评",@"中评",@"差评"];
    for (int i = 0;  i < 3; i ++) {
        x = 40 + width*i + 10 * i;
        UIButton *evaluate_button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [evaluate_button setImage:[UIImage imageNamed:@"Order_flower_gray"] forState:UIControlStateNormal];
        [evaluate_button setImage:[UIImage imageNamed:@"Order_flower"] forState:UIControlStateSelected];
        [evaluate_button setTitle:titleAry[i] forState:UIControlStateNormal];
        [evaluate_button setTitleColor:[UIColor colorWithR:253 g:123 b:172] forState:UIControlStateSelected];
        [evaluate_button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        evaluate_button.imageEdgeInsets = UIEdgeInsetsMake(5,5,5,10);
        [self addSubview:evaluate_button];
        evaluate_button.tag = 10 + i;
        [evaluate_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            evaluate_button.selected = YES;
            _selectTag = 10;
        }
    }
    
    //线
    x = 0;
    y = _proImgView.bottom + 10;
    width = SCREEN_WIDTH;
    height = 0.5;
    _lineView.frame = CGRectMake(x, y, width, height);
    
    
    //评价内容
    x = 10;
    y = _lineView.bottom + 5;
    width = SCREEN_WIDTH - 2*x;
    height = 80;
    _contentTextView.frame = CGRectMake(x, y, width, height);
    
    
    
    
}

#pragma mark ----
- (void)buttonAction:(UIButton *)button{
    [ByCoreToolAlert showOnlyText:[NSString stringWithFormat:@"%ld",(long)button.tag]];
    // 若是当前选中的button 直接返回
    if (button.tag == _selectTag) {
        return;
    }
    
    // 取消上次选中的按钮
    UIButton *select_button = [self viewWithTag:_selectTag];
    select_button.selected = NO;
    [select_button setImage:[UIImage imageNamed:@"Order_flower_gray"] forState:UIControlStateNormal];
    
    //记录当前选中的按钮
    button.selected = !button.selected;
    if (button.selected){
        _selectTag = button.tag;
        [button setImage:[UIImage imageNamed:@"Order_flower"] forState:UIControlStateSelected];
    }else{
        [button setImage:[UIImage imageNamed:@"Order_flower_gray"] forState:UIControlStateNormal];
    }
}

@end
